<template>
  <div>
    <el-dialog title="编辑" :visible.sync="dialogVisibleEdit" width="45%">
      <div>
        <el-form ref="formEdit" :model="editForm" label-width="80px">
          <el-form-item label="请选择套餐分类">
            <el-cascader
              v-model="editId"
              :options="fatherOptions"
              :props="{
                label: 'name',
                value: 'id',
                checkStrictly: true,
                children: 'children',
                expandTrigger: 'click',
              }"
              @change="handleChangeType"
            ></el-cascader>
          </el-form-item>

          <el-form-item label="原价">
            <el-input
              v-model="editForm.lineation_price"
              type="number"
              placeholder="请输入原价"
            ></el-input>
          </el-form-item>

          <el-form-item label="优惠价格">
            <el-input
              v-model="editForm.price"
              type="number"
              placeholder="请输入优惠价格"
            ></el-input>
          </el-form-item>

          <el-form-item label="权重">
            <el-input
              v-model="editForm.weight"
              placeholder="请输入权重"
            ></el-input>
          </el-form-item>

          <el-form-item label="封面图">
            <el-upload
              style="margin-right: 30px"
              class="upload-demo"
              :action="uploadCoverImgUrl"
              :on-success="onSuccessFileCover"
              :on-error="onErrorFileCover"
              :on-remove="onRemoveFileCover"
              :data="{ type: '13' }"
              list-type="text"
            >
              <el-button size="small" type="primary">上传封面图</el-button>
            </el-upload>

            <div v-if="editForm.cover_img">
              <img
                :src="editForm.cover_img"
                alt=""
                style="width: 120px; height: 120px"
              />
            </div>
          </el-form-item>
          <el-form-item label="详情图">
            <tinymce ref="myEditor" @input="input"></tinymce>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogEdit">取 消</el-button>
        <el-button type="primary" @click="sureDialogEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tinymce from "@/components/Tinymce/index.vue";
export default {
  props: ["packageDetails", "tableDataRecharge"],
  components: {
    tinymce,
  },
  data() {
    return {
      dialogVisibleEdit: false,
      editForm: {
        weight: "",
        cover_img: "",
        detail_img: "",
        type: "",
      },
      fatherOptions: [],
      editId: "",
      uploadCoverImgUrl: process.env.VUE_APP_BASE_API + "/app/uploadFiles",
    };
  },
  watch: {
    packageDetails: {
      handler(newVal) {
        this.editForm = newVal;
        this.editId = newVal.type;
      },
    },

    tableDataRecharge: {
      handler(newVal) {
        this.fatherOptions = newVal;
      },
    },
  },
  methods: {
    handleChangeType(val) {
      this.editForm.type = this.editId[val.length - 1];
    },

    // 确定
    sureDialogEdit() {
      this.$emit("sureDialogEdit", this.editForm);
    },

    // 取消
    cancelDialogEdit() {
      this.$emit("cancelDialogEdit", "");
    },

    // 上传主图
    onSuccessFileCover(val) {
      this.editForm.cover_img = val.data.path;
    },
    onErrorFileCover(val) {
      this.apkValue = val.data.path;
    },

    onRemoveFileCover() {
      this.editForm.cover_img = "";
    },

    // 详情图
    input(val) {
      this.editForm.detail_img = val;
    },
  },
};
</script>
<style scoped lang="scss"></style>
